#import <VisionCamera/FrameProcessorPlugin.h>
#import <VisionCamera/FrameProcessorPluginRegistry.h>
#import "measure-Swift.h"
#import "ARCorePlugin.h"

@interface ARCorePlugin () {
    id _swiftPlugin;
}
@end

@implementation ARCorePlugin

+ (void)load {
    [FrameProcessorPluginRegistry addFrameProcessorPlugin:@"arcore" withInitializer:^FrameProcessorPlugin * _Nonnull(VisionCameraProxyHolder * _Nonnull proxy, NSDictionary * _Nullable options) {
        return [[ARCorePlugin alloc] initWithProxy:proxy withOptions:options];
    }];
}

- (instancetype)initWithProxy:(VisionCameraProxyHolder*)proxy withOptions:(NSDictionary*)options {
    self = [super initWithProxy:proxy withOptions:options];
    if (self) {
        // 初始化Swift版本的插件
        Class swiftPluginClass = NSClassFromString(@"measure.ARCoreSwiftPlugin");
        if (swiftPluginClass) {
            _swiftPlugin = [[swiftPluginClass alloc] init];
            // 初始化ARKit会话
            if ([_swiftPlugin respondsToSelector:@selector(initializeSession)]) {
                [_swiftPlugin performSelector:@selector(initializeSession)];
            }
        }
    }
    return self;
}

- (NSDictionary*)callback:(Frame*)frame withArguments:(NSDictionary*)arguments {
    // 调用Swift插件的callback方法
    if (_swiftPlugin && [_swiftPlugin respondsToSelector:@selector(callback:withArguments:)]) {
        return [_swiftPlugin callback:frame withArguments:arguments];
    }
    return @{@"error": @"Swift plugin not initialized"};
}

@end